package com.tl.khadmin.controller;
 
import java.util.List;
import java.util.Map;

import javax.annotation.Resource; 
import javax.servlet.http.HttpSession;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.tl.khadmin.bean.OrderRefund;
import com.tl.khadmin.bean.Product;
import com.tl.khadmin.bean.Supplier;
import com.tl.khadmin.bean.dto.ProductDetailDto;
import com.tl.khadmin.bean.dto.ProductListDto;
import com.tl.khadmin.bean.dto.ProductSelectListDto;
import com.tl.khadmin.page.Page;
import com.tl.khadmin.service.ProductIService;
import com.tl.khadmin.util.CommonUtil;
import com.tl.khadmin.util.Constant;

@RestController
@RequestMapping(value = "/product")
public class ProductController {
	
	@Resource
	private ProductIService productService;  
	
	/**
	 * 审核
	 * @param session 
	 * @return
	 */
	@RequestMapping(value = "/audit") 
	@ResponseBody
	public String audit(HttpSession session, Product product) {
		return productService.audit(session, product);
	}
	
	@RequestMapping(value = "/querybyid")
	@ResponseBody
	public Map<String, Object> queryProductById(Integer id) {
		if(id==null){
			return null;
		}
		return productService.queryProductById(id);
	}
	
	/** 
	 * @param param lastType末级商品类型
	 * @return
	 */
	@RequestMapping(value="/list") 
	public Map<String, List<ProductSelectListDto>> listProductSelect(HttpSession session, @RequestParam Map<String ,Object> param){
		return CommonUtil.returnResultCodeJson("productList", productService.listProductSelect(session, param));
	}

	@RequestMapping(value = "/showproductbypage")
	public Map<String, Page<ProductListDto>> showProductByPage(HttpSession session, @RequestParam Map<String, Object> params) { 
		Integer pageNum = Integer.parseInt(params.get("pageNum").toString());
		Integer pageSize = Integer.parseInt(params.get("pageSize").toString());
		return CommonUtil.returnResultCodeJson("result", productService.showProductByPage(session, pageNum, pageSize, params));
	}
	
	@RequestMapping(value = "/addproduct")
	@ResponseBody
	public Map<String, Object> addProduct(HttpSession session, ProductDetailDto producto){
		return productService.saveProduct(session, producto);
	} 
	
	@RequestMapping(value = "/updateproduct")
	@ResponseBody
	public String updateProduct(HttpSession session, Product product){
		return productService.updateProduct(session, product);
	} 
	
	@RequestMapping(value = "/updatestate")
	@ResponseBody
	public String updateShelveState(HttpSession session, Integer id,Integer shelveState) {
		if(id==null || shelveState==null ){
			return CommonUtil.returnMapResultByRows(0, "","参数错误，请重试");
		}
		Supplier supplier = (Supplier) session.getAttribute(Constant.USER_SUPPLIER);
		if(supplier.getIsInterior() != 1) {
			return CommonUtil.returnMapResultByRows(0, "","无权限");
		}
		int ret = productService.updateShelveState(id,shelveState);
		return CommonUtil.returnMapResultByRows(ret, "修改成功","修改失败");
	}
	
	/**
	 *  保存商品图片，包含主图、详情图
	 * @param session
	 * @param files
	 * @param productId
	 * @return
	 */
	@RequestMapping(value="/saveproductpic")  
	@ResponseBody
	public Map<String, Object> saveProductPic(HttpSession session, @RequestParam("file") MultipartFile[] file){
		String path = session.getServletContext().getRealPath("/upload/productimg");
		return productService.saveProductPic(path,file); 
	}

	@RequestMapping(value = "/deleteproductbyid")
	@ResponseBody
	public String deleteProductById(Integer id) { 
		return productService.deleteProductById(id);
	}

	@RequestMapping(value = "/deleteproductbyids")
	@ResponseBody
	public String deleteProductByIds(HttpSession session,String ids) {
		if(ids==null){
			return CommonUtil.returnMapResultByRows(0, "","未勾选删除商品");
		}
		return productService.deleteProductByIds(ids);
	}

}
